/**
 * Create final table for print preview and submit it to page for print
 *
 * param string myform form name
*/

function submitform(myform)
{
	curent_table = document.getElementById('print_table');
	table = curent_table.cloneNode(true);
	tblBody = table.firstChild;
	var rows = table.firstChild.rows;
	var m=0;
	for(i = 0; i < rows.length; i++)
  	{
		rows_cells = rows[i].cells;
	  	if(rows_cells[0].firstChild.checked)
	  	{
	  		rows[i].removeChild(rows_cells[0]);
	  		rows[i].removeChild(rows_cells[2]);

	  	}
	  	else
	  	{
	  		tblBody.removeChild(rows[i]);i--;
	  	}
  	}
	
	hidden = document.getElementById('final_table');
	var cell = document.createElement("td");
	cell.appendChild(table);
	hidden.value = cell.innerHTML;
	document.forms[myform].submit(); 
}

/**
 * Moving manipulations for rows
 *
 * param rowobject row current row 
 * param int move direction for move
*/
function moverow(row, move)
{
	var curent_index = row.rowIndex; 
	var new_index = curent_index+move;
	var tbody = row.parentNode;
	if(new_index>=0&& new_index < tbody.childNodes.length)
	{
		
		new_row = row.parentNode.rows[new_index];
		var row_1 = row.cloneNode(true);
		var row_2 = new_row.cloneNode(true);

		for(i = 0; i < row.childNodes.length;)
		row.removeChild(row.childNodes[i]);
		for(i = 0; i < new_row.childNodes.length;)
		new_row.removeChild(new_row.childNodes[i]);

		
		for(i = 0; i < row_1.childNodes.length;)
		new_row.appendChild(row_1.childNodes[i]);
		for(i = 0; i < row_2.childNodes.length;)
		row.appendChild(row_2.childNodes[i]);

	}
}

/**
 * Set unset border for table
 *
 * param bool check indicate state of checkbox
 * 
*/
function change_border(check)
{

	var color;
	if(check==true)
	color = '#000';
	else
	color = '#FFF';
	table = document.getElementById('print_table');
	table.style.border = '1px solid '+color ;
	var rows = table.firstChild.rows;
	for(i = 0; i < rows.length; i++)
  	{
		rows_cells = rows[i].cells;
	  	rows_cells[1].style.borderLeft = '1px solid '+color ;
	    rows_cells[2].style.borderLeft = '1px solid '+color;
	    rows_cells[2].style.borderRight = '1px solid '+color;
	    rows_cells[1].style.borderTop = '1px solid '+color;
	    rows_cells[2].style.borderTop = '1px solid '+color;
	    rows_cells[2].style.fontSize="12px";
	    
	    if (i==rows.length-1)
	    {
		    rows_cells[1].style.borderBottom = '1px solid '+color;
		    rows_cells[2].style.borderBottom = '1px solid '+color;
	    }
  	}
}

/**
 * Change font size for table cells
 *
 * param int column indicates column index
 * param int move indicates direction for increase or decrease
 * 
*/

function changeFontSize(move, column)
{
	table = document.getElementById('print_table');
	var input_text = document.getElementById('font'+column);
    var new_value = parseInt(input_text.value) + move;
    if(new_value>=0)
    {
	    input_text.value = new_value;
		var rows = table.firstChild.rows;
		var m=0;
		for(i = 0; i < rows.length; i++)
	  	{
			rows_cells = rows[i].cells;
		  	rows_cells[column].style.fontSize = new_value+"px" ;
	
		}
    }
}


/**
 * Change padding for table cells
 *
 * param int move indicates direction for increase or decrease
 * 
*/
function changePadding(move)
{
	table = document.getElementById('print_table');
	var input_text = document.getElementById('cell_padding');
    var new_value = parseInt(input_text.value) + move;
    if(new_value>=0)
    {
	    input_text.value = new_value;
	    table.cellPadding = new_value;

    }
}	

/**
 * Set/unset bold style for label fields
 *
 * param bool check indicate state of checkbox
 * 
*/

function makeBold(check)
{
	table = document.getElementById('print_table');
	var bold;
	if(check)
	bold = "700";
	else
	bold = '';
	var rows = table.firstChild.rows;
	for(i = 0; i < rows.length; i++)
  	{
		rows_cells = rows[i].cells;
	  	rows_cells[1].style.fontWeight = bold ;
	}
}	

function makeReportPrint(id,title,image_url)
{

	table = document.getElementById(id);
	var tblBody = document.createElement("tbody");
	var row = document.createElement("tr");
	var cell = document.createElement("td");
	
	var select_boxes = document.getElementsByTagName('SELECT');
	if(select_boxes)
	for (i = 0; i < select_boxes.length;)
	{
		select_boxes[i].parentNode.style.border = "1px solid #000";
		select_boxes[i].parentNode.innerHTML = select_boxes[i].options[select_boxes[i].selectedIndex].text;
	}	

	var inputs = document.getElementsByTagName('INPUT');
	if(inputs)
	for (i = 0; i < inputs.length;)
	{
		switch (inputs[i].type)
		{
			case "text":
				if(inputs[i].value!= '')
				inputs[i].parentNode.innerHTML = inputs[i].value;
				else
				inputs[i].parentNode.innerHTML = "&nbsp;";
			break;
			case "button":
				inputs[i].parentNode.removeChild(inputs[i]);
			break;
			default:
				i++;
		}
	}	
	
 	cell.appendChild(table);
	row.appendChild(cell);
 	tblBody.appendChild(row);
 	if (document.getElementById('report_title')) document.getElementById('report_title').value = title;
	document.getElementById('print_preview').value = tblBody.innerHTML;
	document.getElementById('is_report').value = 1;
	document.getElementById('form_preview').submit();
}